define(['app'], function(app) {
    app.controller('MarkdownImageController', function(xUtil, $uibModalInstance) {
        var self = this;
        self.imageUrl = '';
        self.imageName = '';

        self.init = function() {
            // Nothing
        }

        self.upload = function(file) {
            if (file != null) {
                if (/\.(jpg|png|gif)$/i.test(file.name)) {
                    self.imageName = file.name;
                    // upload image
                    xUtil.file.upload('/mock/blog/upload', file).then(function(response) {
                        var filename = response.data;
                        if (filename) {
                            self.imageUrl = '/blog/images/' + filename;
                        }
                    });
                } else {
                    xDialog.alert('提示信息', '您上传的不是有效的图片文件！');
                }
            }
        }

        self.insert = function(valid) {
            if (valid) {
                $uibModalInstance.close({ url: self.imageUrl, name: self.imageName });
            }
        }

        self.cancel = function() {
            $uibModalInstance.dismiss();
        }
    });
});
